<?php
/**
 * Created by PhpStorm.
 * User: Rag_Panda
 * Date: 2015/12/9
 * Time: 14:16
 */

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use BaseClass\Component\Article\ArticleSubject;
use Illuminate\Support\Facades\Session;

class SubjectController extends Controller
{
    //查询专题信息
    public function sSubject()
    {
        /*
         * $query_limit
         * |-start  起始
         * |-num    结束页数
         * |-class  类别
         * |-sort   排序
         * |-search 搜索关键字
         * |-user   特殊用户（不写检查所有用户，会检查是否有管理员权限，也可设置为0自动改成当前session用户）
         * |-reverse 是否逆转排序即倒序
         * |
         *
         *
         * $return_data
         * |-status 是否成功
         * |-message 消息
         * |-data   数据 DB返回的二维结构
         * */
        $query_limit = Request::input("query_limit");
        //判断当前用户是否登录，也就是session是否为空
        if(session("user.user_id",null) == null)
        {
           //为空就直接返回错误数据
           $return_data['status'] = false;
           $return_data['message'] = "你没有权限查看";
            return response()->json($return_data);
        }
        //如果session不为空，检查是否为0，可以的话置为当前session
        if(session("user.user_id") != $query_limit['user'])
        {
            if($query_limit['user'] == 0)
            {
                //置为当前session
                $query_limit['user'] = session("user.user_id");
            }
            else
            {
                $return_data['status'] = false;
                $return_data['message'] = "你不看别人的专题内容";
                return response()->json($return_data);
            }
        }
        $articleSubject = new ArticleSubject(null);
        $subjectArray = $articleSubject->select($query_limit);
        return response()->json($subjectArray);
    }
    //查找当前用户的所有专题
    public function getSubjectByUser()
    {
        $user_id = session("user.user_id");
        $subjectArray = ArticleSubject::getMoreByUser($user_id);
        return response()->json($subjectArray);
    }
    //添加专题
    public function aSubject()
    {
        $info_array = Request::input("subject_name");
        if(ArticleSubject::add($info_array))
        {
            return response()->json(["statue"=>true,"message"=>"添加专题成功"]);
        }
        else
        {
            return response()->json(["statue"=>false,"message"=>"添加专题失败"]);
        }
    }
    //修改专题
    public function uSubject()
    {
        $data['subject_name'] = Request::input("subject_name");
        $subject_id = Request::input("subject_id");
        $articleSubject = new ArticleSubject($subject_id);
        //判断是否是当前用户操作
        if($articleSubject->subject_info->subject_user == session("user.user_id"))
        {
            if($articleSubject->SubjectUpdate($data))
            {
                //修改专题成功
                return response()->json(["statue"=>true,"message"=>"修改专题成功"]);
            }
            else
            {
                return response()->json(["statue"=>true,"message"=>"修改专题失败"]);
            }
        }
        else
        {
            $return_data['status'] = false;
            $return_data['message'] = "你没有权限查看";
            return response()->json($return_data);
        }
    }
    public function dSubject()
    {
        $subject_id = Request::input("subject_id");
        $articleSubject = new ArticleSubject($subject_id);
        if($articleSubject->subject_info->subject_user == session("user.user_id"))
        {
            if($articleSubject->SubjectDelete())
            {
                //修改专题成功
                return response()->json(["statue"=>true,"message"=>"删除专题成功"]);
            }
            else
            {
                return response()->json(["statue"=>true,"message"=>"删除专题失败"]);
            }
        }
        else
        {
            $return_data['status'] = false;
            $return_data['message'] = "你没有权限查看";
            return response()->json($return_data);
        }
    }
    public function aSubjectArticle()
    {
        $article_id = Request::input("article_id");
        $subject_id = Request::input("subject_id");
        $articleSubject = new ArticleSubject($subject_id);
        if($articleSubject->subject_info->subject_user == session("user.user_id"))
        {
            if($articleSubject->SubjectAddArticle($article_id))
            {
                return response()->json(["statue"=>true,"message"=>"给文章添加专题成功"]);
            }
            else
            {
                return response()->json(["statue"=>true,"message"=>"给文章添加专题失败"]);
            }
        }
        else
        {
            $return_data['status'] = false;
            $return_data['message'] = "你没有权限查看";
            return response()->json($return_data);
        }
    }
    public function dSubjectArticle()
    {
        $article_id = Request::input("article_id");
        $subject_id = Request::input("subject_id");
        $articleSubject = new ArticleSubject($subject_id);
        if($articleSubject->subject_info->subject_user == session("user.user_id"))
        {
            if($articleSubject->SubjectRemoveArticle($article_id))
            {
                return response()->json(["statue"=>true,"message"=>"移除文章成功"]);
            }
            else
            {
                return response()->json(["statue"=>true,"message"=>"移除文章失败"]);
            }
        }
        else
        {
            $return_data['status'] = false;
            $return_data['message'] = "你没有权限查看";
            return response()->json($return_data);
        }
    }
    //附加，根据当前专题查找此专题下的所有文章
    public function sArticleToSubject()
    {
        $subject_id = Request::input("subject_id");
        $articleSubject = new ArticleSubject($subject_id);
        return response()->json(["data"=>$articleSubject->subject_info]);
    }
}